import { ElTag } from "element-plus"
import {OptionType} from '@/types'

type Options = {
    successText: string,
    failText: string
}
export const useColumnFormat = ()=>{
    const formatStatus = (status: number|undefined, options: Options = {successText: '启用', failText: '停用'})=>{
        const type = status == 1 ? 'success' : 'danger'
        return (
            <ElTag type={type}>{status == 1 ? options.successText : options.failText}</ElTag>
        )
    }
    const formatOption = (code?: string | number, options?: OptionType[], defaultLabel?: string) =>{
        if(!options){
            return (
                <ElTag type='warning'>{defaultLabel || '无匹配值'}</ElTag>
            )
        }
        const option = options.find(item=>item.value === code)
        if(!option){
            return (
                <ElTag type='warning'>{defaultLabel || '未知状态'}</ElTag>
            )
        }
        return(
            <ElTag type={option.type || 'primary'}>{option.label}</ElTag>
        )
    }
    const formatOptionStr = (code?: number, options?: OptionType[], defaultLabel?: string): string =>{
        if(!options || !code){
            return defaultLabel || '无'
        }
        const option = options.find(item=>item.value === code)
        if(!option || !code){
            return defaultLabel || '无'
        }
        return option.label
    }
    return {
        formatStatus,
        formatOption,
        formatOptionStr
    }
}